/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAOImpl;

import DAO.ILoginDAO;
import Exception.DBConnectionException;
import POJO.Login;
import Util.DBUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author NAT
 */
public class LoginImpl extends BaseImpl implements ILoginDAO {
    private int status;
    private String accType;
    @Override
    public boolean login(String user, String pass) {
        try {            
            Connection con =  DBUtils.getConnection();
            String query = "SELECT * FROM tbluser u, tblaccount a"
                    + " WHERE u.idacc = a.idacc and username = ? and password = ?";
            PreparedStatement pstmt =  con.prepareStatement(query);
            pstmt.setString(1,user);
            pstmt.setString(2,pass);
            ResultSet rs = pstmt.executeQuery();
            //check status login
            rs.next();
            if (rs.getRow() != 1) return false;
            
            status = rs.getInt("status");
            accType = rs.getString("AccType");
            
        } catch (SQLException ex) {
            Logger.getLogger(LoginImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (DBConnectionException ex) {
            Logger.getLogger(LoginImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }

    @Override
    public int getStatus() {
        return status;
    }

    @Override
    public String getAccType() {
        return accType;
    }
    
}
